﻿/*
  In App.xaml:
  <Application.Resources>
      <vm:ViewModelLocatorTemplate xmlns:vm="clr-namespace:AzureTracer.ViewModel"
                                   x:Key="Locator" />
  </Application.Resources>
  
  In the View:
  DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
*/

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using Microsoft.Practices.ServiceLocation;
using AzureTracer.Model;
using AzureTracer.Services;
using AzureTracer.Design;

namespace AzureTracer.ViewModels
{  
    public class ViewModelLocator
    {
        static ViewModelLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

            if (ViewModelBase.IsInDesignModeStatic)            
                SimpleIoc.Default.Register<ILogService, DesignLogService>();            
            else            
                SimpleIoc.Default.Register<ILogService, AzureLogService>();            
            
            if(ViewModelBase.IsInDesignModeStatic)
                SimpleIoc.Default.Register<IDetailViewModel, DesignDetailViewModel>();
            else
                SimpleIoc.Default.Register<IDetailViewModel, DetailViewModel>();
            if (ViewModelBase.IsInDesignModeStatic)
                SimpleIoc.Default.Register<IAppSettings, DesignAppSettings>();
            else
                SimpleIoc.Default.Register<IAppSettings, AppSettings>();

            SimpleIoc.Default.Register<MainViewModel>();
            SimpleIoc.Default.Register<LoginViewModel>();
            SimpleIoc.Default.Register<SettingsViewModel>();            
            SimpleIoc.Default.Register<INavigationService, NavigationService>();
            SimpleIoc.Default.Register<IEmailService, EmailService>(); 
        }

        
        public static MainViewModel Main
        {
            get { return ServiceLocator.Current.GetInstance<MainViewModel>(); }
        }

        public static LoginViewModel Login
        {
            get { return ServiceLocator.Current.GetInstance<LoginViewModel>(); }
        }

        public static SettingsViewModel Settings
        {
            get { return ServiceLocator.Current.GetInstance<SettingsViewModel>(); }
        }

        public static IAppSettings AppSettings
        {
            get { return ServiceLocator.Current.GetInstance<IAppSettings>(); }           
        }

        public static ILogService LogService
        {
            get { return ServiceLocator.Current.GetInstance<ILogService>(); }
        }

        public static IDetailViewModel Detail
        {
            get { return ServiceLocator.Current.GetInstance<IDetailViewModel>(); }
        }

        public static INavigationService NavigationService
        {
            get { return ServiceLocator.Current.GetInstance<INavigationService>(); }
        }

        public static IEmailService EmailService
        {
            get { return ServiceLocator.Current.GetInstance<IEmailService>(); }
        }

        /// <summary>
        /// Cleans up all the resources.
        /// </summary>
        public static void Cleanup()
        {
        }
    }
}